package com.study.seckill;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.HashMap;
import java.util.Map;


@SpringBootApplication
@EnableFeignClients
@MapperScan(basePackages = "com.study.seckill.dao")
@EnableDiscoveryClient
@EnableRabbit
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class ServiceSeckillApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceSeckillApplication.class, args);
    }

//    @Bean
//    public KeyResolver apiKeyResolver() {
//        return exchange ->
//                Mono.just(exchange.getRequest().getPath().value());
//    }

    @Bean
    public Queue myQueueLetter() {
        Map<String,Object> args = new HashMap<>();
        args.put("x-message-ttl", 10000);  //设置ttl（单位是毫秒，表示存活时间，超过时间则失效），延迟队列
        args.put("x-dead-letter-exchange", "wYin-dead-exchange");//消息超过存活时间则发往该交换机
        return new Queue("wYin-letter-queue",true,false,false,args);
    }

    @Bean
    public FanoutExchange fanoutExchangeLetter() {
        FanoutExchange fanoutExchange = new FanoutExchange("wYin-letter-exchange");
        return fanoutExchange;
    }

    @Bean
    public Binding bindingLetter() {
        Binding binding = BindingBuilder.bind(myQueueLetter()).to(fanoutExchangeLetter());
        return binding;
    }


    @Bean
    public Queue myQueueDead() {
        Queue queue = new Queue("wYin-dead-queue");
        return queue;
    }

    @Bean
    public FanoutExchange fanoutExchangeDead() {
        FanoutExchange fanoutExchange = new FanoutExchange("wYin-dead-exchange");
        return fanoutExchange;
    }

    @Bean
    public Binding bindingDead() {
        Binding binding = BindingBuilder.bind(myQueueDead()).to(fanoutExchangeDead());
        return binding;
    }

    @Bean
    public Queue queueUpdateBuyCount(){
        return new Queue("wYin-updateBuyCount-queue");
    }

    @Bean
    public FanoutExchange fanoutExchangeUpdateBuyCount(){
        return new FanoutExchange("wYin-updateBuyCount_exchange");
    }

    @Bean
    public Binding emailBingDing(){
        return BindingBuilder.bind(queueUpdateBuyCount()).to(fanoutExchangeUpdateBuyCount());
    }

    /**
     * - @Configuration：声明配置文件
     * - @EnableSwagger2：开启Swagger2
     * - @EnableSwaggerBootstrapUI：开启SwaggerBootstrapUI
     * - basePackage：声明主包--声明扫描的包
     * - title:文档名称
     * - 团队访问网址：termsOfServiceUrl
     * - 账号信息：Contact
     * - version：版本
     */


    //配置swagger
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.study.seckill"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("课程秒杀接口管理文档")
                .description("课程秒杀接口管理文档")
                .termsOfServiceUrl("http://127.0.0.1:8081/")
                .contact(new Contact("wyin24","","wyin12@qq.com"))
                .version("1.0")
                .build();
    }


}
